package com.guigu.erp.utils;

import com.guigu.erp.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * ClassName:NumberUtils
 * Package:com.guigu.erp.utils
 * Description:
 *
 * @Date:2024/2/26 19:14
 * @Author:chenchong
 */
@Component
public class NumberUtils {


    @Autowired
    private DesignFileService designFileService;
    @Autowired
    private StorageGatherService storageGatherService;

    @Autowired
    private DesignModuleService designModuleService;
    @Autowired
    private StorageCellService storageCellService;
    @Autowired
    private ManufactureManufactureService sanufactureManufactureService;
    @Autowired
    private ManufactureApplyService manufactureApplyService;
 @Autowired
    private StoragePayService storagePayService;

    @Autowired
    private ManufactureDesignProcedureService manufactureDesignProcedureService;

    /**
     * 生成产品编号 : 100+1类编号+2类编号+3类编号+6位流水号
     *
     * @param firstKindId  I级分类编号
     * @param secondKindId II级分类编号
     * @param thirdKindId  II级分类编号
     * @return
     */
    public String getProductNumber(String firstKindId,
                                   String secondKindId,
                                   String thirdKindId) {
        // 拼接类型编号100
        StringBuffer buffer = new StringBuffer("100");

        // 拼接各级分类编号
        buffer.append(firstKindId)
                .append(secondKindId)
                .append(thirdKindId);

        // 获取当前最大的流水号
        String flowNumber = designFileService.getMaxFlowNumber();

        if (flowNumber == null) {
            return buffer.append("000001").toString();
        }
        int temp = Integer.parseInt(flowNumber);
        temp++;
        // 补齐6位字符串
        String newFlowNumber = String.format("%06d", temp);
        String number = buffer.append(newFlowNumber).toString();
        return number;
    }

    public String getTypeNumber(String type) {

        StringBuffer buffer = new StringBuffer(type);

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String year = sdf.format(date);
        buffer.append(year);
// 根据类型获取对应设计单的最大的设计单编号
        String designId = "";
        switch (type) {
            case "200":
                designId = designModuleService.getMaxDesignId();
                break;
            case "201":
                designId = manufactureDesignProcedureService.getMaxprocedure();
                break;
            case "400":
                designId = storageCellService.getMaxprocedure();
                break;
            case "401":
                designId = storageGatherService.getMaxprocedure();
                break;
            case "300":
                designId = manufactureApplyService.getMaxprocedure();
                break;
            case "301":
                designId = sanufactureManufactureService.getMaxprocedure();
                break;
                case "402":
                designId = storagePayService.getMaxprocedure();
                break;
        }
        if (designId == null) {
            return buffer.append("0001").toString();
        }

        //获取当前设计单编号的4位最大流水号
        String substring = designId.substring(11);
        //获取当前设计单编号的年份
        String substring1 = designId.substring(3, 7);
        String number = "";
        if (substring1.equals(year.substring(0, 4))) {
            int temp = Integer.parseInt(substring);
            temp++;
            //补齐4位字符串
            String newFlowName = String.format("%04d", temp);
            number = buffer.append(newFlowName).toString();
        } else {
            number = buffer.append("0001").toString();
        }
        return number;


    }
}